from algos.CTA_PROD import CTA_PROD as STRATEGY
from utility.objects import *
from utility.portfolio import Portfolio
import time
from pathlib import Path

class my_portfolio(Portfolio):


    def display(self):
        import matplotlib.pyplot as plt

        ax1 = plt.subplot(211)
        ax1.set_title("dailyNetValue")
        ax1.plot(self.daily_net_value.index, self.daily_net_value)

        # ax2 = plt.subplot(312, sharex=ax1)
        # ax2.set_title("parameters")
        # ax2.plot(self.inst_performance.n,self.inst_performance.sharpe)

        ax3 = plt.subplot(212, sharex=ax1)
        ax3.set_title("mkt_price")
        ax3.plot(self.symbol.data.mkt_data[["close"]])
        plt.show()

        plt.plot(self.inst_performance.n,self.inst_performance.net_pl)
        plt.title("net_pl")
        plt.show()


if __name__ == '__main__':

    t1 = time.time()
    # prepare symbol data
    # begin = datetime(2018,1, 1)
    # end = datetime(2021, 4, 30)

    begin = datetime(2022,1, 1)
    end = datetime(2022, 2, 10)

    #
    # begin = datetime(2019,1, 1)
    # end = datetime(2019, 12, 1)

    # # prepare symbol data
    # begin = datetime(2021,1, 1)
    # end = datetime(2021, 12, 30)

    portfilio = my_portfolio("CTA")
    #["BTCUSDT","ETHUSDT","LTCUSDT","DOTUSDT","FILUSDT","NEARUSDT","SOLUSDT","EOSUSDT","ETCUSDT","ADAUSDT"] "BCHUSDT","KNCUSDT","LINKUSDT"
    symbol = "BAKEUSDT"
    vol_c = 0.01

    exchange = 'binance'
    data_path = "G:\\trading_2025\\backtest_-py\\data\\binance\\full_data"
    data_path = Path.cwd().parent / "data" / exchange / "full_data"
    file_name = f"{symbol.upper()}-spot"
    file_path = data_path / f'{file_name}.csv'


    data = Mktdata(file_path, "1h", begin, end)
    symbol_object = Symbol(symbol, data)

    setattr(portfilio,"symbol",symbol_object)
    n1 = np.arange(40, 100 + 1,4)
    n2 = np.arange(100, 300 + 1, 10)
    n = np.append(n1,n2)
    # n = n2
    # n = n * 4
    # n = np.arange(16, 48 + 1, 4)
    n = np.arange(20, 80 + 1, 4)

    n = np.arange(72, 80 + 1, 4)

    vols = np.arange(0.2, 3, 0.1)
    for i in n:
        params = {"n": i,
                  "k1": 1,
                  "k2": 1,
                  "position_limit": 0.05,
                  "init_equity": 10000,
                  "fee_rate": -0.001,
                  "taker_fee": -0.001,
                  "vol_c": vol_c,
                  "vol_mod": 1,
                  "min_length": 3,
                  "stop_loss": None,
                  "take_profit": None,
                  "trailling": 8,
                  "trailling_max": 0.8,
                  "exit_mode": 1,
                  "fraction_filter": 1,
                  }

        name = "CTA" + symbol + str(i)
        algo = STRATEGY(name, symbol_object, params, begin, end)
        portfilio.add_algo(name, algo)


    portfilio.init()
    results = portfilio.run_algos_solo()
    # results = portfilio.run_algos_parallelly()
    portfilio.update_portfolio(results)
    portfilio.cal_performance()
    t2 = time.time()
    msg = "total time" + str((t2 - t1)/60) + "minutes"
    print(msg)
    portfilio.display()


